function obj = solve_mc_f(x, N_d, alpha_price, alpha_population, population_log_d, alpha_distance, distance_d, alpha_1, Y_d, nn, Y_market, prod_pct_d, m1_d, m0_d, N_k)

price_d = x(1:N_d, 1); 
q_d = x((N_d + 1):(2*N_d), 1); 

delta_d = q_d + log(price_d)*alpha_price + alpha_population*population_log_d + alpha_distance*distance_d;
delta_market_counter = delta_d;
expmeanval_market_counter = exp(delta_market_counter);

expmu_d = exp(log(price_d)*alpha_1*Y_d);
expmu_market_counter = expmu_d;

marketForProducts_counter = ones(size(delta_market_counter,1), 1);
sharesum_market_counter = sparse(zeros(1,size(delta_market_counter,1)));
sharesum_market_counter(1,:) = 1;
oo = ones(1,nn);   
numer_counter = (expmeanval_market_counter*oo ).*expmu_market_counter;        
sum1 = sharesum_market_counter*numer_counter;
sum11 = 1./(0+sum1);                      
denom1 = sum11(marketForProducts_counter,:);    
simShare_counter = numer_counter.*denom1;              
EstShare_counter = mean(simShare_counter,2); 
share_d = EstShare_counter;

sum_s = simShare_counter;
sum_s2 = simShare_counter.^2;
alpha_i = alpha_price + alpha_1*Y_market;
sum_alpha_s = alpha_i.*simShare_counter;
sum_alpha_s2 = alpha_i.*(simShare_counter.^2);

mc = exp(m0_d .* (q_d(1:N_k, 1) .^ m1_d));

obj1 = share_d .* mc + (price_d - mc) .* (1/nn) .* [sum(sum_alpha_s,2) - sum(sum_alpha_s2,2)];   
obj2 = - m1_d .* (log(mc) ./ q_d) .* share_d + ((price_d - mc) ./ mc) .* (1/nn) .* [sum(sum_s,2) - sum(sum_s2,2)];   

obj = [obj1; obj2];

end

